package day230903;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/9/3 21:08
 */
public class Solution01 {

    public String addBinary(String a, String b) {
        StringBuilder res = new StringBuilder();
        int i = a.length() - 1;
        int j = b.length() - 1;
        int carry = 0; // 模拟进位
        while(i >= 0 || j >= 0){
            int n1 = i >= 0 ? a.charAt(i) - '0' : 0;
            int n2 = j >= 0 ? b.charAt(j) - '0' : 0;
            int sum = n1 + n2 + carry;
            carry = sum / 2;
            res.append(sum % 2);
            i--;
            j--;
        }
        if(carry == 1){
            res.append(1);
        }
        return res.reverse().toString();
    }
}
